﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

public class Constants
{
    public const string DATE_TIME_FORMAT = "yyyy-MM-dd";
    public const char PADD_CHAR = '0';

    public const string DB_PATH = @"db\";

    public const string DB_EXT = "tape";

    public const string MSG_ERROR_INVALID_FORMAT = "Inputs are in invalid format !";
    public const string MSG_Q_WISH_TO_UPDATE = "Wish to update ?";

    public const int SERIAL_NUMBER_LENGTH = 7;

    public const string STATUS_NEW = "NEW";
    public const string STATUS_EXISTS = "EXISTS";

    public const int INDEX_TAPE_SERIAL = 0;
    public const int INDEX_TAPE_DATES = 1;
    public const int INDEX_TAPE_STATUS = 2;

    public static bool folderExists(string path)
    {
        return System.IO.Directory.Exists(path);
    }

    public static bool fileExists(string fileName)
    {
        return File.Exists(fileName);
    }

    public static bool createFolder(string path)
    {
        if (folderExists(path) == false)
        {
            try
            {
                Directory.CreateDirectory(path);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        return true;
        
    }

    public static bool createFile(string fileName, string content)
    {
        try
        {
            using (StreamWriter writer = new StreamWriter(fileName, false))
            {
                writer.WriteLine(content);
            }
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }

    public static string readLineFromAFile(string fileName)
    {
        try
        {
            using (var reader = new StreamReader(fileName))
            {
                string line=null;
                while ((line = reader.ReadLine()) != null)
                {
                    return line;
                }
            }
            return null;
        }
        catch (Exception)
        {
            return null;
        }
    }

    public static List<string> getFileList(string path)
    {
        List<string> fileList = null;
        String[] files = System.IO.Directory.GetFiles(path,string.Format("*.{0}",DB_EXT), SearchOption.TopDirectoryOnly);

        if (files.Length > 0)
        {
            fileList = new List<string>();
            foreach (var file in files)
            {
                fileList.Add(Path.GetFileNameWithoutExtension(file));
            }
        }
        return fileList;
    }

    public static string getStatus(bool existsStatus)
    {
        if (existsStatus)
            return STATUS_EXISTS;
        else
            return STATUS_NEW;
    }

    public static string buildFileName(string filePrefix)
    {
        return string.Format("{0}.{1}", filePrefix, DB_EXT);
    }
}

